<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/07/13 15:01
 * Desc: 会员登录
 */
class Controller_Pc_Api_V2_Member_Login extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Member_Login
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Member_Login
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('member_login', $this->request_params);

        // 服务
        $this->service = new Service_Api_Member_Login();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 账号登录
    public function action_login_by_user()
    {
        try {
            $this->validation->login_by_user()->check();

            $this->result['data'] = $this->service->login_by_user($this->request_params);
            $this->result['msg']  = '登录成功';
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 微信小程序登陆
    public function action_login_by_mini_program()
    {
        try {
            $this->validation->login_by_mini_program()->check();

            $this->result = $this->service->login_by_mini_program($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 微信小程序获取当前用户的手机号码
    public function action_get_phone_by_mini_program()
    {
        try {
            $this->validation->get_phone_by_mini_program()->check();

            $this->result['data'] = $this->service->get_phone_by_mini_program($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 头条小程序登陆
    public function action_login_by_toutiao()
    {
        try {
            $this->validation->login_by_toutiao()->check();

            $this->result = $this->service->login_by_toutiao($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 头条小程序获取当前用户的手机号码
    public function action_get_phone_by_toutiao_program()
    {
        try {
            $this->validation->get_phone_by_toutiao_program()->check();

            $this->result['data'] = $this->service->get_phone_by_toutiao_program($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 支付宝小程序验证登陆
    public function action_login_by_alipay()
    {
        try {
            $this->validation->login_by_alipay()->check();

            $this->result['data'] = $this->service->login_by_alipay($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 根据手机号码登录
    public function action_login_by_phone()
    {
        try {
            $this->validation->login_by_phone()->check();

            $this->result = $this->service->login_by_phone($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


}